package cn.hzw.graffiti;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * Created by zhouwei on 2017/5/12.
 */
public class GraffitiScaleController {

    private int mTouchMode;
    private float mOldScale, mOldDist, mNewDist, mToucheCentreXOnGraffiti,
            mToucheCentreYOnGraffiti, mTouchCentreX, mTouchCentreY;// 双指距离

//    private float mTouchLastX, mTouchLastY;
    private float mScale = 1;
    private final float mMaxScale = 3.5f; // 最大缩放倍数
    private final float mMinScale = 0.25f; // 最小缩放倍数
    private int mTouchSlop;

    private GraffitiView mGraffitiView;

    protected GraffitiScaleController(GraffitiView view) {
        mGraffitiView = view;
        mTouchSlop = ViewConfiguration.get(view.getContext()).getScaledTouchSlop();

        mGraffitiView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        mTouchMode = 1;
                        return true;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mTouchMode = 0;
                        return true;
                    case MotionEvent.ACTION_MOVE:

                        // 双指缩放
                        if (mTouchMode > 1) {

                            // 两点滑动时的距离
                            mNewDist = spacing(event);

                            if (Math.abs(mNewDist - mOldDist) >= mTouchSlop) {

                                float scale = mNewDist / mOldDist;
                                mScale = mOldScale * scale;

                                if (mScale > mMaxScale) {
                                    mScale = mMaxScale;
                                }

                                if (mScale < mMinScale) { // 最小倍数
                                    mScale = mMinScale;
                                }

                                // 更新缩放
                                update(mTouchCentreX, mToucheCentreXOnGraffiti, mTouchCentreY, mToucheCentreYOnGraffiti);
                            }

                            return false;
                        }

                    case MotionEvent.ACTION_POINTER_UP:
                        mTouchMode -= 1;
                        return true;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        mTouchMode += 1;
                        mOldScale = mGraffitiView.getScale();
                        mOldDist = spacing(event);// 两点按下时的距离
                        mTouchCentreX = (event.getX(0) + event.getX(1)) / 2;// 不用减trans
                        mTouchCentreY = (event.getY(0) + event.getY(1)) / 2;
                        mToucheCentreXOnGraffiti = mGraffitiView.toX(mTouchCentreX);
                        mToucheCentreYOnGraffiti = mGraffitiView.toY(mTouchCentreY);
                        return true;
                }

                return true;
            }
        });
    }

    public boolean onScaleTouch(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mTouchMode = 1;
                return false;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mTouchMode = 0;
                return false;
            case MotionEvent.ACTION_MOVE:

                // 双指缩放
                if (mTouchMode > 1) {

                    // 两点滑动时的距离
                    mNewDist = spacing(event);

                    if (Math.abs(mNewDist - mOldDist) >= mTouchSlop) {

                        float scale = mNewDist / mOldDist;
                        mScale = mOldScale * scale;

                        if (mScale > mMaxScale) {
                            mScale = mMaxScale;
                        }

                        if (mScale < mMinScale) { // 最小倍数
                            mScale = mMinScale;
                        }

                        // 更新缩放
                        update(mTouchCentreX, mToucheCentreXOnGraffiti, mTouchCentreY, mToucheCentreYOnGraffiti);
                    }

                    return true;
                }
                return true;
            case MotionEvent.ACTION_POINTER_UP:
                mTouchMode -= 1;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                mTouchMode += 1;
                mOldScale = mGraffitiView.getScale();
                mOldDist = spacing(event);// 两点按下时的距离
                mTouchCentreX = (event.getX(0) + event.getX(1)) / 2;// 不用减trans
                mTouchCentreY = (event.getY(0) + event.getY(1)) / 2;
                mToucheCentreXOnGraffiti = mGraffitiView.toX(mTouchCentreX);
                mToucheCentreYOnGraffiti = mGraffitiView.toY(mTouchCentreY);
                break;
        }

        return false;
    }

    private void update(final float fx, final float tx, final float fy, final float ty) {

        // 围绕坐标(0,0)缩放图片
        mGraffitiView.setScale(mScale);

        // 缩放后，偏移图片，以产生围绕某个点缩放的效果
        float transX = mGraffitiView.toTransX(fx, tx);
        float transY = mGraffitiView.toTransY(fy, ty);

        mGraffitiView.setTrans(transX, transY);
    }

    /**
     * 计算两指间的距离
     *
     * @param event
     * @return
     */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

}
